At this stage of the
course, you should be familiar with the
process for establishing a connection between
the front-end Python-based application and a back-end MySQL database. Your next question might be, what action can I take now that I've established
this connection? Well, one key action
you can undertake with a MySQL Python connection is to create
databases and tables. In this video, you'll
explore the process for creating databases and tables in the MySQL back-end
database using Python. Little Lemon uses
the MySQL Connector, Python client or API to communicate with
their MySQL database. They need to communicate
with the MySQL database to store and retrieve data for
use in analytics reports. Little Lemon can then
use these reports to help make data-driven
decisions for the business. The next step in
the development of this application is to create a database and tables in
which they can store data. Let's see if you
can help them out. Connection between
Little Lemons's existing MySQL database and the Python application
has already been established with the
connection of object. The first step is to
create a cursor object that lets you communicate
with the MySQL database. You'll learn more about
cursors in later lessons. For now, you just need to
know that a cursor object can be created by invoking
the cursor module from the connection object. Cursor object points
the Python application to the location in
the MySQL database, where the required
data is stored. Once you have a cursor object, you can run queries to
the MySQL database. The cursor accesses and
executes module that carries the SQL queries as Python strings the
MySQL database. You will learn more
about cursors, at a later stage at this course. Now it's time to
create a new database, create a SQL statement
as a Python string and pass it to a variable
called create_database_query. The statement must create a
database called Little Lemon. You can use triple quotes to change your statement
to Python string. You can also use single
quotes to create a string, but the advantage of triple
quotes is that you can use them to split this SQL
query into multiple lines. It's much easier to read and manage SQL query
over multiple lines. You know how SQL
query that you can run using the execute
method from the cursor, pass the variable create_database_query
as an argument to the execute method in
the cursor object. Execute this code to create
the Little Lemon database. Next, you need to set
the database for use. First step is to create
your SQL query as a Python string and pass it to a variable called
use_database_query. The query lets you make use
of the Little Lemon database through the use command and
the name of the database. Then pass the variable as an argument, the execute method. You now have a new
database ready to use. The next step is to create
tables for the database. Create a variable called create_menuitem_table
for your SQL query, then create your SQL query as a Python string so that you
can pass it to the variable. Create the query using the create table
command and create a table called menu items. The columns that Little Lemon need in this table
includes item ID, name, type and price. The item ID column lets hold the ID for each
item in the menu. It's assigned an
integer datatype and renders it all increment. This means that a
new ID is assigned to each item in numeric order. The name and type columns display the name of each item in the menu and the type
of cuisine that is associated with both the name, the type bonds are
assigned to that type of VARCHAR and calculated to
200 and 100 respectively. The price column must have split the price of each
item in the menu, it's an integer datatype. The item ID is assigned
the table's primary key. Run the SQL query with triple quotes to
convert it to a string, then create the create
menu item table by invoking the executive
cursor object. Cursor.execute
create_menuitem_table. You can use this same method to create further tables
within the database, just update your SQL
query as needed. Conclusion, Little
Lemon now have a new database and tables
in their MySQL database. You should now be familiar
with the process for creating a database and table in MySQL data back-end
database using Python while developing a Python-based
front-end application.